#include "ColorBlendAttachment.h"
#include "../../Templates/NewObject.h"
#include "../../Misc/Crc.h"
#include "../RenderState.h"

VkPipelineColorBlendAttachmentState ColorBlendAttachment::GetColorBlendAttachment() const
{
	return mColorBlendAttachment;
}

void ColorBlendAttachment::ReleaseResource()
{
	DeleteObject<ColorBlendAttachment>(this);
}

ColorBlendAttachment::Builder& ColorBlendAttachment::Builder::SetBlendEnable(VkBool32 blendEnable) noexcept
{
	mImpl->colorBlendAttachment.blendEnable = blendEnable;
	return *this;
}

ColorBlendAttachment::Builder& ColorBlendAttachment::Builder::SetSrcColorBlendFactor(VkBlendFactor srcColorBlendFactor) noexcept
{
	mImpl->colorBlendAttachment.srcColorBlendFactor = srcColorBlendFactor;
	return *this;
}

ColorBlendAttachment::Builder& ColorBlendAttachment::Builder::SetDstColorBlendFactor(VkBlendFactor dstColorBlendFactor) noexcept
{
	mImpl->colorBlendAttachment.dstColorBlendFactor = dstColorBlendFactor;
	return *this;
}

ColorBlendAttachment::Builder& ColorBlendAttachment::Builder::SetColorBlendOp(VkBlendOp colorBlendOp) noexcept
{
	mImpl->colorBlendAttachment.colorBlendOp = colorBlendOp;
	return *this;
}

ColorBlendAttachment::Builder& ColorBlendAttachment::Builder::SetSrcAlphaBlendFactor(VkBlendFactor srcAlphaBlendFactor) noexcept
{
	mImpl->colorBlendAttachment.srcAlphaBlendFactor = srcAlphaBlendFactor;
	return *this;
}

ColorBlendAttachment::Builder& ColorBlendAttachment::Builder::SetDstAlphaBlendFactor(VkBlendFactor dstAlphaBlendFactor) noexcept
{
	mImpl->colorBlendAttachment.dstAlphaBlendFactor = dstAlphaBlendFactor;
	return *this;
}

ColorBlendAttachment::Builder& ColorBlendAttachment::Builder::SetAlphaBlendOp(VkBlendOp alphaBlendOp) noexcept
{
	mImpl->colorBlendAttachment.alphaBlendOp = alphaBlendOp;
	return *this;
}

ColorBlendAttachment::Builder& ColorBlendAttachment::Builder::SetColorWriteMask(VkBlendOp alphaBlendOp) noexcept
{
	mImpl->colorBlendAttachment.alphaBlendOp = alphaBlendOp;
	return *this;
}

RefCountPtr<ColorBlendAttachment> ColorBlendAttachment::Builder::Build()
{
	const uint32 hash = GetHash();
	RefCountPtr<ColorBlendAttachment> colorBlendAttachment = GRenderState->GetColorBlendAttachmentByHash(hash);
	if (colorBlendAttachment.IsValid())
		return colorBlendAttachment;
	colorBlendAttachment = RefCountPtr<ColorBlendAttachment>(NewObject<ColorBlendAttachment>(*mImpl));
	GRenderState->RegisterColorBlendAttachment(hash, colorBlendAttachment);
	return colorBlendAttachment;
}

uint32 ColorBlendAttachment::Builder::GetHash() const
{
	return CRC32((uint8*)&mImpl->colorBlendAttachment, sizeof(VkPipelineColorBlendAttachmentState));
}

ColorBlendAttachment::ColorBlendAttachment(BuilderDetails& buildDetails)
{
	mColorBlendAttachment = buildDetails.colorBlendAttachment;
}